<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initConfig()
    {
        Manager::setPathRoot(ROOT_PATH);
    }

    protected function _initManager()
    {
        Manager::setBootstrap($this);
    }

    protected function _initPageCache()
    {
        $options = $this->getOptions();
        $pageFrontendOptions = array(
            'lifetime' => $options['cache']['lifetime'],
            'debug_header' => $options['cache']['debug_header'],
            'default_options' => array(
                'cache_with_get_variables' => true,
                'cache_with_post_variables' => false,
                'cache_with_session_variables' => false,
                'cache_with_files_variables' => false,
                'cache_with_cookie_variables' => true,
                'make_id_with_get_variables' => true,
                'make_id_with_post_variables' => false,
                'make_id_with_session_variables' => false,
                'make_id_with_files_variables' => false,
                'make_id_with_cookie_variables' => false,
            ),
            'regexps' => array(
                '' => array('cache' => true),
                '^/$' => array('cache' => true),
                '^/captcha' => array('cache' => false),
                '^/.+$' => array('cache' => true))
        );

        $pageBackendOptions = array('cache_dir' => Manager::getPathCache() . '/page');

        $pageCache = Zend_Cache::factory('Page',
            'File',
            $pageFrontendOptions,
            $pageBackendOptions);

        Manager::setPageCache($pageCache);
        $pageCache->start();

        return $pageCache;
    }

    protected function _initCache()
    {
        $options = $this->getOptions();

        $frontendOptions = array(
            'lifetime' => $options['cache']['lifetime'],
            'automatic_serialization' => true
        );

        $backendOptions = array(
            'cache_dir' => Manager::getPathCache() . '/core',
        );
        $cache = Zend_Cache::factory('Core',
            'File',
            $frontendOptions,
            $backendOptions);

        Manager::setCache($cache);

        return $cache;
    }

    protected function _initLoader()
    {
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath' => APPLICATION_PATH,
            'namespace' => '',
            'resourceTypes' => array(
                'models' => array(
                    'path' => 'models/',
                    'namespace' => 'Models',
                ),
                'data' => array(
                    'path' => 'datas/',
                    'namespace' => 'Datas',
                ),
                'services' => array(
                    'path' => 'services/',
                    'namespace' => 'Services',
                ),
                'plugins' => array(
                    'path' => 'plugins/',
                    'namespace' => 'Plugins',
                ),
                'managers' => array(
                    'path' => 'managers/',
                    'namespace' => 'Managers',
                ),
                'forms' => array(
                    'path' => 'forms/',
                    'namespace' => 'Forms',
                ),
                'utils' => array(
                    'path' => 'utils/',
                    'namespace' => 'Utils',
                ),
                'factory' => array(
                    'path' => 'factory/',
                    'namespace' => 'Factory',
                ),
                'validate' => array(
                    'path' => 'validate/',
                    'namespace' => 'Validate',
                ),
                'controllers' => array(
                    'path' => 'controllers/',
                    'namespace' => 'Controllers',
                ),
            ),
        ));

        $resourceLoaderModels = new Zend_Loader_Autoloader_Resource(array(
            'basePath' => APPLICATION_PATH . '/models',
            'namespace' => 'Models',
            'resourceTypes' => array(
                'content' => array(
                    'path' => 'content/',
                    'namespace' => 'Content',
                ),
                'core' => array(
                    'path' => 'core/',
                    'namespace' => 'Core',
                ),
                'management' => array(
                    'path' => 'management/',
                    'namespace' => 'Management',
                ),
                'utils' => array(
                    'path' => 'utils/',
                    'namespace' => 'utils',
                ),
            ),
        ));

        $resourceLoaderForms = new Zend_Loader_Autoloader_Resource(array(
            'basePath' => APPLICATION_PATH . '/forms',
            'namespace' => 'Forms',
            'resourceTypes' => array(
                'decorators' => array(
                    'path' => 'decorators/',
                    'namespace' => 'Decorators',
                ),
                'elements' => array(
                    'path' => 'elements/',
                    'namespace' => 'Elements',
                ),
            ),
        ));

        $resourceLoaderDatas = new Zend_Loader_Autoloader_Resource(array(
            'basePath' => APPLICATION_PATH . '/datas',
            'namespace' => 'Datas',
            'resourceTypes' => array(
                'decorators' => array(
                    'path' => 'core/',
                    'namespace' => 'Core',
                ),
            ),
        ));

        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->pushAutoloader($resourceLoader);
        $autoloader->pushAutoloader($resourceLoaderModels);
        $autoloader->pushAutoloader($resourceLoaderForms);
        $autoloader->pushAutoloader($resourceLoaderDatas);
    }

    protected function _initFactories()
    {
        $factoryModel = new Factory_FactoryModelDefault();
        Manager::setFactoryModel($factoryModel);

        $factoryForm = new Factory_FactoryFormDefault();
        Manager::setFactoryForm($factoryForm);
    }

    protected function _initMapper()
    {
        $mapper = new Utils_Mapper();
        Manager::setMapper($mapper);
    }

    protected function _initLocale()
    {
        $locale = new Zend_Locale();
        Manager::setLocale($locale);

        return $locale;
    }

    protected function _initTranslate()
    {
        $this->bootstrap('cache');

        $traductions = Manager::loadCache('traductions', function()
        {
            return Models_Core_Langage::getTraductions();
        });

        $translate = null;
        if (count($traductions) >= 1) {
            reset($traductions);
            $translate = new Zend_Translate('array', current($traductions), key($traductions));
            next($traductions);
        }

        if (!is_null($translate)) {
            while (list($code, $traduction) = each($traductions)) {
                $translate->addTranslation($traduction, $code);
            }
        }

        // Utile pour le helper Translate
        Manager::setTranslate($translate);

        return $translate;
    }

    protected function _initAcces()
    {
        $acl_ini = Manager::getPathApplication() . '/configs/acl.ini';
        $acl = new Managers_Acces($acl_ini);
        Manager::setAcl($acl);

        return $acl;
    }

    protected function _initView()
    {
        $view = new Zend_View($this->getOptions());

        /* DOJO */
        Zend_Dojo::enableView($view);

        /* JQUERY */
        ZendX_JQuery::enableView($view);

        /* TRANSITION (GALERIES IMAGES) */
        $view->gallerie = new Utils_Gallerie($view);

        /* DOCTYPE */
        $view->doctype('XHTML5');

        /* TITLE */
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('Gite Nouette');
        // Initialisation par défaut du title
        $view->title = '';
        $view->lang = Manager::getLocale();

        /* META	*/
        $view->headMeta()
            ->appendName('author', $view->translate('meta.author'))
            ->appendName('keywords', $view->translate('meta.keywords'))
            ->appendName('description', $view->translate('meta.description'))
            ->setCharset('UTF-8' );

        /* LINK */
        $urlBaseCss = $view->serverUrl($view->baseUrl('css/gite.nouette.css'));
        $urlIco = $view->serverUrl($view->baseUrl('img/gite.nouette.ico'));
        $urlPng = $view->serverUrl($view->baseUrl('img/gite.nouette.png'));
        $view->headLink()
            ->offsetSetStylesheet(100, $urlBaseCss)
            ->headLink(array('rel' => 'shortcut icon', 'href' => $urlIco), 'PREPEND')
            ->headLink(array('rel' => 'icon', 'type' => 'image/x-icon', 'href' => $urlIco), 'PREPEND')
            ->headLink(array('rel' => 'icon', 'type' => 'image/png', 'href' => $urlPng, 'PREPEND'))
            ->headLink(array('rel' => 'publisher', 'href' => 'https://plus.google.com/108434266452464511772', 'PREPEND'));
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);

        return $view;
    }

    protected function _initPlugins()
    {
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');

        $pluginInitialisation = new Plugins_InitialisationPlugin();
        $front->registerPlugin($pluginInitialisation);

        $pluginError = new Plugins_ErrorPlugin();
        $front->registerPlugin($pluginError);

        $pluginNavigation = new Plugins_NavigationPlugin();
        $front->registerPlugin($pluginNavigation);

        //		$this->bootstrap('acces');
        //		$pluginAuthentification = new Plugins_AuthentificationPlugin(Manager::getAcl());
        //		$front->registerPlugin($pluginAuthentification) ;
    }
}